package com.lightkits.wms.domain.mainpage.service.impl;

import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.database.dao.WmsOrganizationBaseMapper;
import com.lightkits.wms.database.model.OrganizationViewModel;
import com.lightkits.wms.dto.mainpage.model.OrganizationInfoDTO;
import com.lightkits.wms.domain.mainpage.service.OrganizationDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * OrganizationDomainServiceImpl
 *
 * @author menxiao.wu@hand-china.com
 * Created on 2019-02-20 15:47
 */
@Service
public class OrganizationDomainServiceImpl implements OrganizationDomainService {
    private final WmsOrganizationBaseMapper mapper;

    @Autowired
    public OrganizationDomainServiceImpl(WmsOrganizationBaseMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public List<OrganizationInfoDTO> listOrganizationInfo(List<String> levels) {
        List<OrganizationViewModel> organizationViewModels = mapper.listOrganizationInfo(levels);
        return getDTO(organizationViewModels);
    }

    private List<OrganizationInfoDTO> getDTO(List<OrganizationViewModel> organizationViewModels) {
        return ObjectUtil.isEmpty(organizationViewModels) ? Collections.EMPTY_LIST : organizationViewModels.stream().map(
                t -> {
                    OrganizationInfoDTO dto = new OrganizationInfoDTO();
                    dto.setOrganizationId(t.getOrganizationId());
                    dto.setOrganizationCode(t.getOrganizationCode());
                    dto.setDescription(t.getDescription());
                    return dto;
                }
        ).collect(Collectors.toList());
    }
}
